//初始化路由实例，并导出
import Vue from 'vue'
import VueRouter from 'vue-router'
import auth from '@/utils/auth'

//导入组件-绝对路径
const  Login  =() =>  import( '@/views/login')
const  Layout  =() =>  import( '@/views/Layout')
const  Welcome  =() =>  import( '@/views/welcome')
const  NotFound  =() =>  import( '@/views/404')
const  Article  =() =>  import( '@/views/article')
const  Image  =() =>  import( '@/views/image')
const  Publish  =() =>  import( '@/views/publish')
const  Comment  =() =>  import( '@/views/comment')
const  Fans  =() =>  import( '@/views/fans')
const  Setting =() =>  import( '@/views/setting')

Vue.use(VueRouter)

//这个位置会有很多的路由规则
const routes=[
  {path:'/login',component:Login},
  //首页架子
  {
    path:'/' , 
    component: Layout,
    children:[
        //欢迎页面
        {path:'/',component:Welcome},
        //内容管理
        {path:'/article',component:Article },
        // 素材管理
        {path:'/image',component:Image},
        //发布文章
        {path:'/publish',component:Publish},
        //评论管理
        {path:'/comment',component:Comment},
        {path:'/fans',component:Fans},
        {path:'/setting',component:Setting},
    ]
  },
  //最后，通配规则。404组件
  {path:'*', component:NotFound}
]
const router = new VueRouter({
  routes
})

//导航守卫功能（每次跳转路由）
router.beforeEach((to,from,next) => {
  //to 即将跳转到的路由对象
  //from 正在离开的路由对象
  //next() 下一步，放行   next('地址')拦截，跳转去哪儿
  const user = auth.getUser()
  //不是访问登录，且没有token信息，不能访问其他页面，拦截登录
  if(to.path !== '/login' && !user.token) return next('/login')
  next()
})

export default router